package com.ricchieri.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@Table(name="charter")
public class Charter extends BaseEntity {
	private static final long serialVersionUID = 5957764419716135447L;
	
	@Column(name="patent", nullable=false, length=10)
	private String patent;
	
	@Column(name="places", nullable=false)
	private int places = 0;
	
	@Column(name="kilometers", nullable=false)
	private Long kilometers = new Long(0);
	
	@OneToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "charterType", nullable = false)
	private CharterType charterType;
	
	@Column(name="observation", nullable=false, length=100)
	private String observation;
	
	@Column(name="start", nullable=false)
	private Date start;
	
	@Column(name="finish", nullable=true)
	private Date finish;	
	
	@Column(name="charterNumber", nullable=false)
	private int charterNumber = 0;
	
	@OneToMany(fetch = FetchType.LAZY, mappedBy = "charter", cascade=CascadeType.ALL)
	private List<CharterExpense> charterExpenses;
	
	public void setPatent(String patent) {
		this.patent = patent;
	}
	
	public int getPlaces() {
		return places;
	}
	
	public void setPlaces(int places) {
		this.places = places;
	}
	
	public CharterType getCharterType() {
		return charterType;
	}
	
	public void setCharterType(CharterType charterType) {
		this.charterType = charterType;
	}
	
	public Date getStart() {
		return start;
	}
	
	public void setStart(Date start) {
		this.start = start;
	}
	
	public Date getFinish() {
		return finish;
	}
	
	public void setFinish(Date finish) {
		this.finish = finish;
	}
	
	public String getPatent() {
		return patent;
	}
	
	public String getObservation() {
		return observation;
	}
	
	public void setObservation(String observation) {
		this.observation = observation;
	}

	public Long getKilometers() {
		return kilometers;
	}

	public void setKilometers(Long kilometers) {
		this.kilometers = kilometers;
	}

	public int getCharterNumber() {
		return charterNumber;
	}

	public void setCharterNumber(int charterNumber) {
		this.charterNumber = charterNumber;
	}

	public List<CharterExpense> getCharterExpenses() {
		return charterExpenses;
	}

	public void setCharterExpenses(List<CharterExpense> charterExpenses) {
		this.charterExpenses = charterExpenses;
	}
	
	public void addCharterExpense(CharterExpense expense) {
		if(getCharterExpenses() == null || getCharterExpenses().isEmpty()) {
			setCharterExpenses(new ArrayList<CharterExpense>());
		}
		getCharterExpenses().add(expense);
	}
}